package repository.relsys.services.datacentral;

import java.io.FileInputStream;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;




import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.ModelFactory;

public class FileDataProvider implements DataProvider {

	private String dataSourceUrl;
	private String rdfLang;
	
	public FileDataProvider(String dataSourceUrl, String rdfLang) {
		super();
		this.dataSourceUrl = dataSourceUrl;
		this.rdfLang = rdfLang;
	}
	
	@Override
	public Model getData() {
		
		Model m = ModelFactory.createDefaultModel();
		m.setNsPrefix("lt","http://diplomski.nelakolundzija.org/LTontology.rdf#");
		m.setNsPrefix("swc","http://data.semanticweb.org/ns/swc/ontology#");
		m.setNsPrefix("jenabean", "http://thewebsemantic.com/");
		m.setNsPrefix("dc", "http://purl.org/dc/elements/1.1/");
		m.setNsPrefix("xsd", "http://www.w3.org/2001/XMLSchema#");
		m.setNsPrefix("rdfs", "http://www.w3.org/2000/01/rdf-schema#");
		
		File dataFile = new File(dataSourceUrl);
		
		if(dataFile.exists()){
			try {
				m.read(new FileInputStream(dataSourceUrl),
						"http://diplomski.nelakolundzija.org/LTontology.rdf#", rdfLang);
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return m;

		
	}

	@Override
	public void updateDataSource(Model m) {
		try {
			
			m.write(new FileOutputStream(dataSourceUrl), rdfLang, "http://diplomski.nelakolundzija.org/LTontology.rdf#");
		} catch (FileNotFoundException e) {
			System.err.println( e.getMessage() );
			e.printStackTrace();
		}
	}

}
